/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   31.03.2010
 * @version SVN: $Id: ProductSearchAction.java 76 2010-04-11 17:49:32Z christian-kerl@web.de $
 */
package org.cakis.server.action.impl;

import java.util.List;

import org.cakis.server.Names;
import org.cakis.server.model.Product;
import org.cakis.server.service.ProductLookupService;
import org.cakis.server.service.ProductLookupServiceRegistry;
import org.cakis.server.service.ProductNotFoundException;
import org.cakis.server.service.ProductService;
import org.cakis.server.service.StockUnitNotFoundException;
import org.hibernate.validator.NotNull;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.Controller;
import org.jboss.seam.international.StatusMessage.Severity;

/**
 * ProductSearchAction
 *
 * @author Christian Kerl
 */
@Name("productSearch")
@Scope(ScopeType.PAGE)
public class ProductSearchAction extends Controller {
    private static final long serialVersionUID = 1L;

    @In("facesMessages")
    private FacesMessages msg;

    @In(Names.ProductService)
    private ProductService productService;

    @In(Names.ProductLookupServiceRegistry)
    private ProductLookupServiceRegistry lookupServiceRegistry;

    private String eanNumber;
    private Product result;

    @Factory
    @DataModel
    public List<ProductLookupService> getLookupServices() {
        return lookupServiceRegistry.getServices();
    }

    @NotNull
    public String getEanNumber() {
        return eanNumber;
    }

    public void setEanNumber(String eanNumber) {
        this.eanNumber = eanNumber;
    }

    @Out(value="productSearchResult", required=false, scope=ScopeType.CONVERSATION)
    public Product getResult() {
        return result;
    }

    @Begin
    public Product search() {
        try {
            Product info = productService.getProduct(eanNumber);

            if(info.getId() == null) {
                result = info;

                msg.add(Severity.INFO, "Product '#0' with EAN Number '#1' found!", info.getName(), info.getEanNumber());
            } else {
                msg.add(Severity.WARN, "Product '#0' with EAN Number '#1' already in database!", info.getName(), info.getEanNumber());
            }
        } catch (ProductNotFoundException e) {
            msg.add(Severity.ERROR, "No Product with EAN Number '#0' found!", eanNumber);
        } catch (StockUnitNotFoundException e) {
            msg.add(Severity.ERROR, "No StockUnit with Name or Symbol '#0' found!", e.getUnit());
        }

        return result;
    }
}
